/*
 * The MIT License
 *
 * Copyright (c) 2010, Marc Schwerdtfeger (marc@schwerdtfeger.de)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.loosix.core.modules.logger
{
    import org.loosix.core.Loosix;
    import org.loosix.core.lxc_internal;
    import org.loosix.core.module.events.ModuleEvent;
    import org.loosix.core.utils.ModuleUtil;

    use namespace lxc_internal;

    public class LoggerBridge implements LoggerService
    {
        private static var _instance:LoggerBridge;

        private var _service:LoggerService;
        private var _interfaceName:String = ModuleUtil.getInterfaceName(LoggerService);

        /**
         * Die internen Module loggen die Ereignisse im Framework mit. Wenn jedoch der LoggerService
         * unregistriert wird, k�nnte es eine NullPointerException geben, weil das Modul nicht mehr vorhanden ist.
         * Um dies zu vermeiden gibt es die LoggerBridge, die die Logs empf�ngt und falls der LoggerService
         * vorhanden ist weiterleitet.
         * @param se privater Konstruktor
         */
        public function LoggerBridge(se:LoosixEnforcer)
        {
            init();
        } // endConstructor

        /**
         * Die LoggerBridge pr�ft ob der LoggerService gestartet oder gestoppt wurde.
         */
        private function init():void
        {
            _service = Loosix.moduleManager.getModule(_interfaceName) as LoggerService;

            // listening if our service is stopped or updated
            Loosix.topicDispatcher.addEventListener(ModuleEvent.TOPIC,
                                                    ModuleEvent.MODULE_STOPPING,
                                                    moduleStoppingHandler);

            Loosix.topicDispatcher.addEventListener(ModuleEvent.TOPIC,
                                                    ModuleEvent.MODULE_STARTED,
                                                    moduleStartedHandler);
        } // endFunc

        private function moduleStartedHandler(event:ModuleEvent):void
        {
            if(event.module.interfaceName == _interfaceName )
            {
                _service = Loosix.moduleManager.getModule(_interfaceName) as LoggerService;
            } // endIf
        } // endFunc

        private function moduleStoppingHandler(event:ModuleEvent):void
        {        
            if(event.module.interfaceName ==_interfaceName )
            {
                 // my loggerservice is stopping!
                 _service = null;
            } // endIf
        } // endFunc

        lxc_internal static function getInstance():LoggerBridge
        {
            if (_instance == null) _instance = new LoggerBridge(new LoosixEnforcer());
            return _instance;
        } // endFunc

        public function log(message:String, target:Object, object:Object = null):void
        {
            if(_service) _service.log(message,target,object);
        }

        public function info(message:String, target:Object, object:Object = null):void
        {
            if(_service) _service.info(message,target,object);
        }

        public function debug(message:String, target:Object, object:Object = null):void
        {
            if(_service) _service.debug(message,target,object);
        }

        public function warn(message:String, target:Object, object:Object = null):void
        {
            if(_service) _service.warn(message,target,object);
        }

        public function error(message:String, target:Object, object:Object = null):void
        {
            if(_service) _service.error(message,target,object);
        }

        public function fatal(message:String, target:Object, object:Object = null):void
        {
            if(_service) _service.fatal(message,target,object);
        }

        public function set level(level:uint):void
        {
            if(_service) _service.level = level;
        }

        public function get level():uint
        {
            if(_service) return _service.level;
            else return 0;            
        } // endFunc

        public function dispose():void
        {
            Loosix.topicDispatcher.removeEventListener(ModuleEvent.TOPIC,
                                                       ModuleEvent.MODULE_STOPPING,
                                                       moduleStoppingHandler);

            Loosix.topicDispatcher.removeEventListener(ModuleEvent.TOPIC,
                                                       ModuleEvent.MODULE_STARTED,
                                                       moduleStartedHandler);
        } // endFunc

    } // endClass

} // endPackage
class LoosixEnforcer{};